<?php

/**
 * Description of adm
 *
 * @author Меркулов Илья aka Bit0reZ
 */

class adm extends CI_Controller {
	
	var $uid ; // UID in VVSU
        var $role ; // Role in VVSU
        var $maxParticipantCount = 60; // max users on Webinar
        var $base_url;
	var $env = ENVIRONMENT;
	var $stat;
	var $bbb_url;
	var $salt;
	var $adm;

        function __construct()
	{
		parent::__construct();
		$this->load->config('eclass_cfg');
                $this->load->helper('data');
                $this->load->helper('url');
                $this->load->database('default');
                $this->load->library("session");
                $this->load->model('WebinarDB','wdb');
                $this->load->library('Webinar');
                $this->uid = $this->session->userdata('UID');
                $this->role = $this->session->userdata('role');
                $this->base_url = base_url();
		$this->bbb_url = $this->config->item('bbb_url');
		$this->salt = $this->config->item('salt');
		$this->adm = $this->session->userdata('adm');
        }
	
	public function index()
	{
		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//RU" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<title>Админко v0.1</title>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			<script type="text/javascript" src="'.$this->base_url.'js/jquery-1.5.1.min.js"></script>
			</head>
			<body>
			';
		if(!$this->uid) $this->login();
		else if ($this->role != 'admin') echo "Error! You don`t have admin privileges!";
		else
		{
			$data = $this->wdb->past($this->uid, true);
			echo "<table>";
			foreach($data as $key => $value)
			{
				echo "<tr>";
				foreach($value as $val)
					echo "<td>".$val."</td>";
				echo "<td><input type='checkbox' value='".$value['ID']."' /></td>";
				echo "</tr>";
			}
			echo "</table>";
			echo "<input type='submit' value='Удалить' />";
			echo "
				<script type='text/javascript'>
					$(document).ready(function(){
						$('input:submit').click(function(){
							var arr = [];
							$.each($('input:checked'),function(){
								arr += ' ';
								arr += $(this).val();
							});
							$.post('".$this->base_url."adm/delete',
								{id: arr},
								function(data){
									if(data.status == 'done')
										document.location = '".$this->base_url."adm';
								},
								'json');
						});
					});
				</script>
				";
		}
	}
	
	public function login()
	{
		if(!$this->uid)
		{
			$login = $this->input->post('login');
			$pass = $this->input->post('pass');
			if(!$login && !$pass)
			{
				echo "
					<form action='adm/login' method='post'>
					Авторизуйтесь:<br />
						<input type='text' name='login' /><br />
						<input type='password' name='pass' /><br />
						<input type='submit' />
					</form>
				";
			}
			else
			{
				$this->load->model('check');
				$data = $this->check->checkVVSUAccount($login, $pass, 'admin');
				if(is_array($data))
				{
					$this->session->set_userdata(array('UID' => $data['UID']));
					$this->session->set_userdata(array('surname' => $data['surname']));
					$this->session->set_userdata(array('firstname' => $data['firstname']));
			                $this->session->set_userdata(array('secondname' => $data['secondname']));
					$this->session->set_userdata(array('role' => $data['role']));
					redirect($this->base_url."adm");
				}
				else
				{
					echo "Error! You don`t have admin privileges!";
				}
			}
		}
		
		else if($this->role != 'admin')
		{
			echo "Error! You don`t have admin privileges!";
		}
		/*
		else if(!$this->adm)
		{
			$pass = $this->input->post('pass');
			if(!$pass)
			{
				echo "
					<form action='adm/login' method='post'>
					Пароль для доступа к админке: <br />
						<input type='password' name='pass' /><br />
						<input type='submit' />
					</form>
				";
			}
			else
			{
				if($pass == 'sup3r4dm1n')
				{
					$this->session->set_userdata(array('adm' => true));
					redirect($this->base_url."adm");
				}
				else
				{
					echo "Error!";
				}
			}
		}*/
		else
		{
			$this->index();
		}
	}
	
	public function delete()
	{
		$i = 1;
		$data = $this->input->post('id');
		preg_match_all('/[\S]*/', $data, $match);
		while(isset($match[0][$i]))
		{
			$this->wdb->delPast($match[0][$i]);
			log_message('info', $this->uid.' delete webinar '.$match[0][$i]);
			$i+=2;
		}
		$this->output->set_output(json_encode(array( 'status' => 'done')));
	}
	
}

?>
